package com.xzz.finger;

import java.util.Arrays;

/**
 * @author: hhz
 * @create: 2022-02-21 10:55
 * 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
 **/
public class Exchange {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(exchange(new int[]{2, 4, 6})));
        System.out.println(Arrays.toString(exchange(new int[]{1, 2, 3,4})));
    }

    public static int[] exchange(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        while (left < right) {
            while (left < right && nums[left] % 2 != 0) {
                left++;
            }
            while (left < right && nums[right] % 2 == 0) {
                right--;
            }
            int temp = nums[left];
            nums[left] = nums[right];
            nums[right] = temp;
        }
        return nums;
    }
}
